//
//  YDUserExperienceTableViewCell.m
//  IngCard
//
//  Created by kf on 2017/8/30.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDUserExperienceTableViewCell.h"


@implementation YDUserExperienceCellDataSource

-(Class) viewClass
{
    return [YDUserExperienceTableViewCell class];
}

@end

@interface YDUserExperienceTableViewCell()
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UILabel *detailLabel;
@property(nonatomic, strong) UILabel *contentLabel;
@property(nonatomic, strong) UIImageView *headerView;
@end

@implementation YDUserExperienceTableViewCell

+(CGFloat)viewHeight:(YDUserExperienceCellDataSource *) dataSource
{
    return dataSource.height ?: 60;
}


- (void) initSubviews
{
    [super initSubviews];

    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(self.contentView).offset(10);
    }];
    
    [self.contentView addSubview:self.detailLabel];
    [self.detailLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.titleLabel.mas_bottom);
    }];
    
    [self.contentView addSubview:self.contentLabel];
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.detailLabel.mas_bottom).offset(1);
    }];
    
    [self.contentView addSubview:self.headerView];
    [self.headerView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-20);
    }];
}

-(void) onViewModelChanged
{
    YDUserExperienceCellDataSource *dataSource = (YDUserExperienceCellDataSource *)self.dataSource;
    if (dataSource)
    {
        self.titleLabel.text = dataSource.title;
        [self.titleLabel sizeToFit];
        
        self.detailLabel.text = dataSource.detail;
        [self.detailLabel sizeToFit];
        
        self.contentLabel.text = dataSource.content;
        [self.contentLabel sizeToFit];
        
        if (dataSource.headerImage)
        {
            float scale = dataSource.headerImage.size.width / dataSource.headerImage.size.height;
            self.headerView.image = [dataSource.headerImage qmui_imageWithScaleToSize:CGSizeMake(36, 26*scale) contentMode:UIViewContentModeScaleToFill];;
        }
    }
}

#pragma mark - lazy loading
-(UILabel *) titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        [_titleLabel setStyle:^(UIView *target) {
            UILABELSTYLE;
            temp.font = UIMediumFontMake(12);
            temp.textColor = UIColorBlack;
            temp.textAlignment = NSTextAlignmentLeft;
        }];
        [_titleLabel sizeToFit];
    }
    return _titleLabel;
}

-(UILabel *) detailLabel
{
    if (!_detailLabel)
    {
        _detailLabel = [[UILabel alloc] init];
        [_detailLabel setStyle:^(UIView *target) {
            UILABELSTYLE;
            temp.font = UIThinFontMake(9);
            temp.textColor = UIColorBlack;
            temp.textAlignment = NSTextAlignmentLeft;
        }];
        [_detailLabel sizeToFit];
    }
    return _detailLabel;
}

-(UILabel *) contentLabel
{
    if (!_contentLabel)
    {
        _contentLabel = [[UILabel alloc] init];
        [_contentLabel setStyle:^(UIView *target) {
            UILABELSTYLE;
            temp.font = UIRegularFontMake(8);
            temp.textColor = UIColorMake(151, 151, 151);
            temp.textAlignment = NSTextAlignmentLeft;
        }];
        [_contentLabel sizeToFit];
    }
    return _contentLabel;
}

-(UIImageView *) headerView
{
    if (!_headerView)
    {
        _headerView = [UIImageView new];
        _headerView.contentMode = UIViewContentModeCenter;
        [_headerView sizeToFit];
    }
    return _headerView;
}

@end
